﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class quytrinh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"] == null)
        {
            Response.Redirect("chuadangnhap.aspx");
        } 
        string sql="";
        connection ketnoi = new connection();
        ketnoi.Open();
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        if (this.IsPostBack == false)
        {
            ViewState["PreviousPage"] = Request.UrlReferrer;
            string sanpham = Request["sanpham"];
            string soluong = Request["soluong"];
            sql = "SELECT * FROM TINHTHANH";

            nguoiban_tinhthanh.DataSource = ketnoi.Ds_table(ds,sql, "TINHTHANH");
            nguoiban_tinhthanh.DataBind();

        }
          
        if (Session["User"] != null)
        {
            string user = Session["User"].ToString();
            sql = "SELECT * FROM ACCOUNT AC, THANHVIEN TV WHERE AC.USERNAME = TV.USERNAME AND AC.USERNAME='" + user + "'";
            dt = ketnoi.return_table(ds,sql, "THANHVIEN");
            foreach (DataRow dr in dt.Rows)
            {
                nguoimua_diachi.Text = dr["diachi"].ToString();
                nguoimua_dienthoai.Text = dr["dienthoai"].ToString();
                nguoimua_email.Text = dr["email"].ToString();
                nguoimua_hoten.Text = dr["hoten"].ToString();
                nguoimua_tinhthanh.Text = dr["tinhthanh"].ToString();
                //nguoimua_tinhthanh.Items.
            }

        }
    }
    public void quytrinh2(object sender, EventArgs e)
    {
        if (nguoiban_diachi.Text == "" || nguoiban_dienthoai.Value == "" || nguoiban_email.Value == "" || nguoiban_hoten.Value == "" || nguoimua_diachi.Text == "" || nguoimua_dienthoai.Text == "" || nguoimua_email.Text == "" || nguoimua_hoten.Text == "")
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Vui lòng điền đầy đủ thông tin')</script>");
        }
        else
        {

            NguoiNhan.diaChi = nguoiban_diachi.Text;
            NguoiNhan.email = nguoiban_email.Value;
            NguoiNhan.soDienThoai = nguoiban_dienthoai.Value;
            NguoiNhan.ten = nguoiban_hoten.Value;
            NguoiNhan.tinh_Thanh_Pho = nguoiban_tinhthanh.SelectedItem.Value;
            /*
            lNguoiNhan.
            lNguoiNhan nguoinhan = new lNguoiNhan();

            nguoinhan.nguoiNhan.ten = nguoiban_hoten.Value;
            nguoinhan.nguoiNhan.diaChi = nguoiban_diachi.Text;
            nguoinhan.nguoiNhan.email = nguoiban_email.Value;
            nguoinhan.nguoiNhan.soDienThoai = nguoiban_dienthoai.Value;
            nguoinhan.nguoiNhan.tinh_Thanh_Pho = nguoiban_tinhthanh.SelectedItem.Value;*/
            if (Request["sanpham"] != null)
            {
                string soluong = Request["soluong"];
                string sanpham = Request["sanpham"];
                Response.Redirect("quytrinh2.aspx?soluong=" + soluong + "&sanpham=" + sanpham);
            }
            if (Request["shop"] != null)
            {
                string shop = Request["shop"];
                Response.Redirect("quytrinh2.aspx?shop=" + shop);
            }
        }
        
    }
    
    public void fillform(object sender, EventArgs e)
    {
        if (nguoiban_checkthongtin.Checked)
        {
            nguoiban_diachi.Text = nguoimua_diachi.Text;
            nguoiban_dienthoai.Value = nguoimua_diachi.Text;
            nguoiban_email.Value = nguoimua_email.Text;
            nguoiban_hoten.Value = nguoimua_hoten.Text;
            //nguoiban_tinhthanh.SelectedIndex = nguoimua_tinhthanh.SelectedIndex;
            int i = 0;
            foreach (ListItem item in nguoiban_tinhthanh.Items)
            {
                if (item.Text == nguoimua_tinhthanh.Text)
                {
                    break;
                }
                else
                    i++;
            }
            nguoiban_tinhthanh.SelectedIndex = i;
            

        }
    }
    public void back(object sender, EventArgs e)
    {
        if (ViewState["PreviousPage"] != null)	//Check if the ViewState 
        //contains Previous page URL
        {
            Response.Redirect(ViewState["PreviousPage"].ToString());//Redirect to 
            //Previous page by retrieving the PreviousPage Url from ViewState.
        }
    }
    
}